package com.drore.logapp.base;

import android.app.Application;
import android.os.Build;

import com.drore.app.LogRecordUtil;
import com.drore.app.utils.AppUtils;
import com.drore.app.utils.LogUtils;
import com.drore.logapp.utils.SPUtil;

import java.io.File;

/**
 * desc
 *
 * @author sixbing
 * @date 2018/11/6 16:33
 */

public class BaseApplication extends Application {
    //私钥
    private static String encryptPrivateKey = "0123456789012345";
    private static BaseApplication application;
    private static SPUtil sp;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        sp = new SPUtil(this);
        initLogan();
    }

    public static BaseApplication getInstance() {
        return application;
    }

    public static SPUtil getSp() {
        return sp;
    }

    @Override
    public void onTerminate() {
        LogRecordUtil.stopLogcatRecord();
        LogUtils.e("BaseApplication", "onTerminate() app stop ");
        super.onTerminate();
    }

    private void initLogan() {
        LogRecordUtil.Builder builder = new LogRecordUtil.Builder()
                .setUpLoadUrl("http://172.16.10.236:8080/logan/insertLoganList")
                .setAppId("FASFWQVDS")
                .setAppVersion(AppUtils.getAppVersion(this))
                .setFileSaveDay(7)
                .setAppCrashPath(getFilePath("crashFile"))
                .setLogcatFilePath(getFilePath("logcat"))
                .setCurrentVersionCode(Build.SERIAL)
                .setMaxAutoUploadCount(50)
                .setEncryptPrivateKey(encryptPrivateKey)
                .build();
        //初始化log日志
        LogRecordUtil.initLog(this, builder);
        // 开启logUtils 打印日志
        LogRecordUtil.openLog(true);
//        主动上传日志（自己决定在什么时候上传）
//        LogRecordUtil.sendLogs();
    }

    public String getFilePath(String foldername) {
        return getApplicationContext().getExternalFilesDir(null).getAbsolutePath()
                + File.separator + foldername + File.separator;
    }
}
